<?php
namespace app\admin\controller;

class Index extends Base{

    public function index(){

        return $this->fetch();
    }
    //系统设置
    public function sysconfig(){
        if(request()->isPost()){
            $data=input('post.');
            $where=[
                'id'=>1
            ];
            $datas=[
                'signed_count'=>$data['signed_count'],
                'notsign_count'=>$data['notsign_count'],
                'signed_money'=>$data['signed_money'],
                'notsign_money'=>$data['notsign_money'],
                'today_benefit' => $data['today_benefit'],
                'cur_money'=>$data['cur_money'],
                'cur_per'=>$data['cur_per'],
//                'cut_money'=>$data['cut_money'],
//                'profit_rate' => $data['profit_rate']
            ];
            $res=model('System')->where($where)->update($datas);
            if($res){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
        $sys=model('System')->sysConfig();
        $this->assign('sys',$sys);
        return $this->fetch();
    }

    /**
     * 挑战活动设置
     */
    public function projectconfig(){
        if(request()->isPost()){
            $data = input('post.');
            $data['profit_rate'] = json_encode($data['profit_rate']);
           $res =  model('System')->update($data);
           if($res){
               $this->success('保存成功');
           }else{
               $this->error('保存失败');
           }
        }
        $sys=model('System')->sysConfig();
        $profit = json_decode($sys['profit_rate'],true);
        $this->assign('profit',$profit);
        $this->assign('sys',$sys);
        return $this->fetch();
    }



    //用户管理
    public function userList(){
        $list=model('User')->getList();
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    /**
     * 编辑用户
     */
    public function useredit($id){
        $where = [
            'uid' =>$id,
            'status' => 0
        ];
        $project =  db('project')->where($where)->find();
        $userinfo = db('user')->find($id);
        if(request()->isPost()){
            $data = input('post.');
            $data['pwd'] = md5($data['pwd']);
            $pro = $data['money'];
          $res1  =  model('user')->allowField(true)->save($data,['id'=>$id]);
          $res2 = db('project')->where($where)->setField('money',$pro);
          $this->success('更新成功',url('index/userlist'));
        }
        $this->assign('project',$project);
        $this->assign('userinfo',$userinfo);
        return $this->fetch();
    }



    //删除用户
    public function userdel($id){
        if(empty($id)){
            $this->error('参数错误','index/userlist');
        }
        $user=model('User');
        $res=$user->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功','index/userlist','',1);
        }else{
            $this->error('删除失败','index/userlist','',1);
        }

    }

    //活动列表
    public function project(){
        $project=model('Project');
        $list=$project->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //打卡列表
    public function itemlist(){
        $projectitem=model('Projectitem');
        $list=$projectitem->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //优惠列表
    public function discounts_list(){
        $projectitem=model('Discounts');
        $list=$projectitem->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //添加优惠券
    public function add_discount(){
        if(request()->isPost()){
            $data=input('post.');
            if(empty($data['money'])||empty($data['surplus'])||empty($data['expire'])){
                $this->error('内容不能为空','','',1);
            }
            $data['add_time']=time();
            $discount=model('Discounts')->create($data)->save();
            if($discount==0){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    //编辑优惠券
    public function edit_discount(){
        if(request()->isPost()){
            $data=input('post.');
            if(empty($data['money'])||empty($data['surplus'])||empty($data['expire'])){
                $this->error('内容不能为空','','',1);
            }
            $datas=[
                'money'=>$data['money'],
                'surplus'=>$data['surplus'],
                'expire'=>$data['expire'],
                'status'=>$data['status']
            ];
            $discount=model('Discounts')->where('id',$data['id'])->update($datas);
            if($discount){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }
        $id=input('id');
        $disInfo=model('Discounts')->where('id',$id)->find();
        $this->assign('disInfo',$disInfo);
        return $this->fetch();
    }

    //删除优惠券
    public function discountdel($id){
        if(empty($id)){
            $this->error('参数错误','index/discounts_list');
        }
        $user=model('Discounts');
        $res=$user->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功','index/discounts_list','',1);
        }else{
            $this->error('删除失败','index/discounts_list','',1);
        }
    }

//    是否设置自动打卡
    public function is_card(){
        $id = input('id');
        $status = input('status');
       $res =  db('user')->where('id',$id)->setField('auto_card',$status);
       if($res){
           if($status==1){
               return 1;
           }else{
               return 0;
           }
       }else{
           return '更新失败';
       }
    }




}